package com.aliengen.thingple.driverapp.ui.activities;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.support.annotation.StringRes;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

import com.aliengen.thingple.beep.DevBeep;
import com.aliengen.thingple.driverapp.R;
import com.aliengen.thingple.driverapp.data.local.Preferences;
import com.aliengen.thingple.driverapp.device.CMCHandler;
import com.aliengen.thingple.driverapp.service.HandsetApplication;

import java.util.Locale;

/**
 * This class is part of the Thingple mobile application for the CMC168 Handheld reader.
 *
 * Copyright AlienGen Limited 2016. All rights reserved.
 *
 * @author Timothé Mermet-Buffet <tim@aliengen.com>
 */
public abstract class AbstractBaseActivity extends AppCompatActivity {
    protected Resources resources;

    protected HandsetApplication mApplication;
    static CMCHandler handler = new CMCHandler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        resources = getResources();
        mApplication = (HandsetApplication) getApplication();
        mApplication.setHandler(handler);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        Configuration config = new Configuration();

        String language = getPreferences().get(Preferences.APP_LANGUAGE, "en");

        Locale locale = new Locale(language);
        Locale.setDefault(locale);
        config.locale = locale;
        getBaseContext().getResources().updateConfiguration(config,
                getBaseContext().getResources().getDisplayMetrics());

        DevBeep.init(this);
    }

    public Preferences getPreferences() {
        return mApplication.getPreferences();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_settings) {
            Intent nextScreen = new Intent(getApplicationContext(), SettingsActivity.class);
            startActivity(nextScreen);
            return true;
        }

        if (id == R.id.action_logout) {
            Intent nextScreen = new Intent(getApplicationContext(), TruckActivity_.class);
            startActivity(nextScreen);
            this.finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void showToast(final String message) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AbstractBaseActivity.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void showToast(@StringRes final int message) {
        this.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(AbstractBaseActivity.this.getApplicationContext(), message, Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void setTextView(TextView textView, int format, String content)
    {
        if(textView == null)
        {
            Log.e("Activity", "setTextView() empty textView!");
            return;
        }
        String message = String.format(getResources().getString(format), content);
        textView.setText(message);
        textView.invalidate();
    }
}
